package cn.actoncode.boot.module.ai.socket.auth;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import jakarta.websocket.OnClose;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import jakarta.websocket.server.PathParam;
import jakarta.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
@ServerEndpoint(value = "/api/socket/login/{sk}")
public class WebLoginSocket {

    private static ConcurrentHashMap<String, Session> sessionMap = new ConcurrentHashMap<>();

    private Session session;

    /**
     * 连接建立成功调用的方法*/
    @OnOpen
    public void onOpen(Session session, @PathParam("sk") String sk) {
        this.session = session;
        log.info("[微信小程序websocket]socketKey:{}",sk +"-->建立连接");
        sessionMap.put(sk, session);
    }
    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose(@PathParam("sk") String sk) {
        log.info("[微信小程序websocket]socketKey:{}",sk +"-->断开连接");
        sessionMap.remove(sk);
    }

    public ConcurrentHashMap<String, Session> getSessionMap() {
        return sessionMap;
    }
}
